import heapq as hp
def kth(nums, k, key = lambda x:x):
    heap = []
    for num in nums:
        if len(heap) < k:
            hp.heappush(heap, (key(num), num))
        else:
            if key(num) > heap[0][0]:
                hp.heapreplace(heap, (key(num), num))
    return heap[0][1]

print(kth([3,2,1,5,6,4] , 2, lambda x:-x))